﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Composite.Presentation.Commands;

namespace ASPItalia.ModelVirtualCasting.Silverlight.BackOffice.Command
{
  public class DoubleClickCommandBehavior : CommandBehaviorBase<DataGrid>
  {
    private long lastClickTicks;

    public DoubleClickCommandBehavior(DataGrid targetObject)
      : base(targetObject)
    {
      targetObject.MouseLeftButtonUp += new MouseButtonEventHandler(targetObject_MouseLeftButtonUp);
    }

    void targetObject_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
      if ((DateTime.Now.Ticks - this.lastClickTicks) < 5000000)
      {
        base.ExecuteCommand();
        this.lastClickTicks = 0;
      }
      this.lastClickTicks = DateTime.Now.Ticks;
    }
  }

  public static class DoubleClick
  {
    public static readonly DependencyProperty DoubleClickBehaviorProperty =
                                    DependencyProperty.RegisterAttached("DoubleClickBehaviorProperty",
                                    typeof(DoubleClickCommandBehavior),
                                    typeof(DoubleClickCommandBehavior),
                                    null);

    public static readonly DependencyProperty CommandProperty =
        DependencyProperty.RegisterAttached("Command",
        typeof(ICommand),
        typeof(DoubleClick),
        new PropertyMetadata(CommandProperty_Changed));

    public static ICommand GetCommand(DependencyObject obj)
    {
      return (ICommand)obj.GetValue(CommandProperty);
    }

    public static void SetCommand(DependencyObject obj, ICommand value)
    {
      obj.SetValue(CommandProperty, value);
    }

    private static void CommandProperty_Changed(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
    {
      DataGrid targetObject = dependencyObject as DataGrid;

      if (targetObject != null)
        GetOrCreateBehavior(targetObject).Command = e.NewValue as ICommand;
    }

    private static DoubleClickCommandBehavior GetOrCreateBehavior(DataGrid targetObject)
    {
      DoubleClickCommandBehavior behavior = targetObject.GetValue(DoubleClickBehaviorProperty) as DoubleClickCommandBehavior;

      if (behavior == null)
      {
        behavior = new DoubleClickCommandBehavior(targetObject);
        targetObject.SetValue(DoubleClickBehaviorProperty, behavior);
      }

      return behavior;
    }

    public static readonly DependencyProperty CommandParameterProperty =
       DependencyProperty.RegisterAttached("CommandParameter",
       typeof(object),
       typeof(DoubleClick),
       new PropertyMetadata(CommandParameterProperty_Changed));

    public static object GetCommandParameter(DependencyObject obj)
    {
      return obj.GetValue(CommandParameterProperty);
    }

    public static void SetCommandParameter(DependencyObject obj, object value)
    {
      obj.SetValue(CommandParameterProperty, value);
    }

    private static void CommandParameterProperty_Changed(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
    {
      var dataGrid = dependencyObject as DataGrid;
      if (dataGrid != null)
      {
        DoubleClickCommandBehavior behavior = GetOrCreateBehavior(dataGrid);
        behavior.CommandParameter = e.NewValue;
      }
    }
  }
}
